import formRules from "../../utils/rules-form"
Component({
  /**
   * 组件的属性列表
   */
  properties: {},
  /**
   * 组件的初始数据
   */
  data: {
    showCheckForm: false,
    autosize: {
      maxHeight: 200,
      minHeight: 100
    },
    checkInfo: {},
    form: {
      id: '',
      ywid: '',
      lx: '',
      flfg: '',
      zgqx: '',
      question: '',
      fileList: []
    },
    zgqxRequired: true,
    isEdit: false, //是否是编辑
    rules: {
      ywid: [{
        required: true,
        message: '请选择检查项目'
      }],
      lx: [{
        required: true,
        message: '请选择检查类型'
      }],
      zgqx: [{
        required: true,
        message: '请选择整改期限'
      }]
    },
    wtid:''
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 打开本身这个弹窗 参数 1-当前选择的检查项 2-已经填写的数据传进来
    open(data) {
      if (data) {
        this.setData({
          form: data,
          isEdit: true
        })
      }
      this.setData({
        showCheckForm: true,
      })
      // this.requestData()
    },
    // 检查类型选择器 change事件
    checkTypeChange(e) {
      if (e.detail.bm == 0) {
        this.data.rules.zgqx[0].required = false
        this.setData({
          'form.lx': e.detail.bm,
          zgqxRequired: false
        })
      } else {
        this.data.rules.zgqx[0].required = true
        this.setData({
          'form.lx': e.detail.bm,
          zgqxRequired: true
        })
      }
    },
    // 检查项目选择器 change事件
    checkItemeChange(e) {
      this.setData({
        'form.ywid': e.detail.ywid,
        'form.ename': e.detail.checkname,
        columnstemplate: e.detail.wtmsList
      })
    },
    // 整改期限 change事件
    zgqxChange(event) {
      this.setData({
        'form.zgqx': event.detail
      })
    },
    // 法律法规 change事件
    flfgChange(event) {
      this.setData({
        'form.flfg': event.detail.id
      })
    },
    // 模板 change事件
    mbChange(event) {
      this.setData({
        'form.question': event.detail.flfg,
        wtid: event.detail.id //保存模板id，用来
      })
    },
    // 问题模板字段监听 小程序不支持复杂的双向绑定
    questionChange(e) {
      this.setData({
        'form.question': e.detail
      })
    },
    //图片上传
    afterRead(event) {
      const form = this.data.form
      let that = this;
      const {
        file
      } = event.detail;
      // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
      wx.uploadFile({
        url: 'https://njaqs.top/securityinspection/fileUpload/uploadFile',
        filePath: file.url,
        name: 'file',
        success(res) {
          const data = JSON.parse(res.data)
          const fileList = form.fileList || []
          fileList.push({
            url: data.fileUrl,
            name: data.filename,
            fileurl: data.url,
            type: 'image'
          })
          form.fileList = fileList
          that.setData({
            form: form
          });
        },
      });
    },
    //删除图片
    delete(event) {
      const form = this.data.form
      form.fileList.splice(event.detail.index, 1)
      this.setData({
        form: form
      });
    },
    // 保存并关闭
    saveAndclose() {
      this.save(1)
    },
    // 保存并新建
    saveAndAdd() {
      this.save(2)
    },
    // 保存数据 其实就是把数据发送到父组件保存起来 类型是必填的
    save(type) {
      formRules(this.data.rules, this.data.form, (status) => {
        if (status) {
          this.triggerEvent('sendMsg', {
            form: this.data.form,
            isEdit: this.data.isEdit
          })
          this.cancel(type)
        }
      })
    },
    // 删除这个检查项
    del() {
      let vm = this;
      wx.showModal({
        title: '提示',
        content: '确定删除吗？',
        success(res) {
          vm.triggerEvent('sendMsg', {
            isDel: true
          })
          vm.cancel(1)
        }
      })
    },
    // 关闭表单弹窗
    cancel(type) {
      this.setData({
        isEdit: false,
        form: {
          id: '',
          ywid: '',
          lx: '',
          flfg: '',
          zgqx: '',
          question: '',
          fileList: []
        },
      })
      if (type == 1) {
        this.setData({
          showCheckForm: false
        })
      }
    },
    // 关闭
    close() {
      this.cancel(1)
    },
  }
})